package com.ruoyi.common;

import com.ruoyi.system.domain.*;

import java.awt.*;
import java.util.ArrayList;
import java.util.List;

public class DrawElementsFactory {

    private float boxWidth = 35;
    private float boxHeight = 35;
    private float leftX = 5;
    private float cyJJ = 15;
    private float rowJJ = 25;
    private float initY = 125;
    private float maxRowCount = 10;
    private boolean isNewRow = true;
    private int drawWordsCount = 0;
    private int drawCYCount = 0;
    private int drawRowIndex = 1;
    private List<List<DrawElements>> drawElementsList;

    //绘制元素集合
    public List<List<DrawElements>> getDrawElementsList() {
        return drawElementsList;
    }

    private Paper paper;

    public DrawElementsFactory(Paper paper) {
        this.paper = paper;
    }

    public DrawElementsFactory createDrawElements() {
        drawElementsList = new ArrayList<>();
        List<DrawElements> drawElements = new ArrayList<>();
        LineDrawElements lineDrawElements = new LineDrawElements();
        //试卷标题
        TextDrawElements textDrawElements = new TextDrawElements();
        textDrawElements.setText(paper.getTitle());
        textDrawElements.setFontType(FontType.大标题);
        textDrawElements.setColor(Color.BLACK);
        textDrawElements.setX(-99);
        textDrawElements.setY(30);
        drawElements.add(textDrawElements);


        if(paper.getExamType() == PaperType.exam || paper.getExamType() == PaperType.examAndAnswer){
            //年级
            textDrawElements = new TextDrawElements();
            textDrawElements.setText("年级");
            textDrawElements.setFontType(FontType.小标题);
            textDrawElements.setColor(Color.BLACK);
            textDrawElements.setX(30);
            textDrawElements.setY(70);
            drawElements.add(textDrawElements);

            //年级划线
            lineDrawElements = new LineDrawElements();
            lineDrawElements.setLineType(LineType.solid);
            lineDrawElements.setColor(Color.BLACK);
            lineDrawElements.setPenWidth(0.5f);
            lineDrawElements.setWidth(60);
            lineDrawElements.setX(45);
            lineDrawElements.setY(85);
            lineDrawElements.setToX(45+60);
            lineDrawElements.setToY(85);
            drawElements.add(lineDrawElements);

            //班级
            textDrawElements = new TextDrawElements();
            textDrawElements.setText("班级");
            textDrawElements.setFontType(FontType.小标题);
            textDrawElements.setColor(Color.BLACK);
            textDrawElements.setX(140);
            textDrawElements.setY(70);
            drawElements.add(textDrawElements);
            //班级划线
            lineDrawElements = new LineDrawElements();
            lineDrawElements.setLineType(LineType.solid);
            lineDrawElements.setColor(Color.BLACK);
            lineDrawElements.setPenWidth(0.5f);
            lineDrawElements.setX(155);
            lineDrawElements.setY(85);
            lineDrawElements.setToX(155 + 60);
            lineDrawElements.setToY(85);
            drawElements.add(lineDrawElements);

            //姓名
            textDrawElements = new TextDrawElements();
            textDrawElements.setText("姓名");
            textDrawElements.setFontType(FontType.小标题);
            textDrawElements.setColor(Color.BLACK);
            textDrawElements.setX(250);
            textDrawElements.setY(70);
            drawElements.add(textDrawElements);
            //姓名划线
            lineDrawElements = new LineDrawElements();
            lineDrawElements.setLineType(LineType.solid);
            lineDrawElements.setColor(Color.BLACK);
            lineDrawElements.setPenWidth(0.5f);
            lineDrawElements.setX(265);
            lineDrawElements.setY(85);
            lineDrawElements.setToX(265+60);
            lineDrawElements.setToY(85);
            drawElements.add(lineDrawElements);

            //日期
            textDrawElements = new TextDrawElements();
            textDrawElements.setText("成绩");
            textDrawElements.setFontType(FontType.小标题);
            textDrawElements.setColor(Color.BLACK);
            textDrawElements.setX(360);
            textDrawElements.setY(70);
            drawElements.add(textDrawElements);
            //日期划线
            lineDrawElements = new LineDrawElements();
            lineDrawElements.setLineType(LineType.solid);
            lineDrawElements.setColor(Color.BLACK);
            lineDrawElements.setPenWidth(0.5f);
            lineDrawElements.setX(375);
            lineDrawElements.setY(85);
            lineDrawElements.setToX(375+60);
            lineDrawElements.setToY(85);
            drawElements.add(lineDrawElements);
        }

        if(paper.getExamType() == PaperType.answer){
            //答案标题
            textDrawElements = new TextDrawElements();
            textDrawElements.setText("答案");
            textDrawElements.setFontType(FontType.大标题);
            textDrawElements.setColor(Color.BLACK);
            textDrawElements.setX(-99);
            textDrawElements.setY(70);
            drawElements.add(textDrawElements);
        }



        RectangleDrawElements rectangleDrawElements;
        TextDrawElements pyTextDrawElements;
        TextDrawElements hzTextDrawElements;
        for (int i = 0; i < this.paper.getPinyinList().size(); i++) {
            for (int j = 0; j < this.paper.getWordList().get(i).size(); j++) {

                if (j == 0 && drawWordsCount + this.paper.getWordList().get(i).size() > 10) {//换行
                    drawWordsCount = 0;
                    drawCYCount = 0;
                    drawRowIndex++;
                    initY = initY + boxHeight + rowJJ;
                    if (drawRowIndex > maxRowCount) {
                        drawRowIndex = 0;
                        initY = 30;
                        drawElementsList.add(drawElements);
                        drawElements = new ArrayList<>();
                    }
                }


                //方格
                rectangleDrawElements = new RectangleDrawElements();
                rectangleDrawElements.setWidth(boxWidth);
                rectangleDrawElements.setHeight(boxHeight);
                rectangleDrawElements.setColor(new Color(paper.getBoxColor().get(0),paper.getBoxColor().get(1),paper.getBoxColor().get(2)));
                rectangleDrawElements.setPenWidth(0.5f);
                rectangleDrawElements.setX(leftX + drawWordsCount * boxWidth + drawCYCount * cyJJ);
                rectangleDrawElements.setY(initY);
                drawElements.add(rectangleDrawElements);

                if(paper.getBoxType() == 2 || paper.getBoxType() == 3){
                    //田字格横线
                    lineDrawElements = new LineDrawElements();
                    lineDrawElements.setLineType(LineType.dash);
                    lineDrawElements.setColor(new Color(paper.getBoxColor().get(0),paper.getBoxColor().get(1),paper.getBoxColor().get(2)));
                    lineDrawElements.setPenWidth(0.5f);
                    lineDrawElements.setX(leftX + drawWordsCount * boxWidth + drawCYCount * cyJJ);
                    lineDrawElements.setY(initY + boxHeight/2);
                    lineDrawElements.setToX(lineDrawElements.getX() + boxWidth);
                    lineDrawElements.setToY(initY + boxHeight/2);
                    drawElements.add(lineDrawElements);

                    //田字格竖线
                    lineDrawElements = new LineDrawElements();
                    lineDrawElements.setLineType(LineType.dash);
                    lineDrawElements.setColor(new Color(paper.getBoxColor().get(0),paper.getBoxColor().get(1),paper.getBoxColor().get(2)));
                    lineDrawElements.setPenWidth(0.5f);
                    lineDrawElements.setX(leftX + drawWordsCount * boxWidth + drawCYCount * cyJJ + boxWidth/2);
                    lineDrawElements.setY(initY);
                    lineDrawElements.setToX(lineDrawElements.getX());
                    lineDrawElements.setToY(initY + boxHeight);
                    drawElements.add(lineDrawElements);
                }


                if(paper.getBoxType() == 3) {
                    //米字格右叉线
                    lineDrawElements = new LineDrawElements();
                    lineDrawElements.setLineType(LineType.dash);
                    lineDrawElements.setColor(new Color(paper.getBoxColor().get(0),paper.getBoxColor().get(1),paper.getBoxColor().get(2)));
                    lineDrawElements.setPenWidth(0.5f);
                    lineDrawElements.setX(leftX + drawWordsCount * boxWidth + drawCYCount * cyJJ);
                    lineDrawElements.setY(initY);
                    lineDrawElements.setToX(lineDrawElements.getX() + boxWidth);
                    lineDrawElements.setToY(initY + boxHeight);
                    drawElements.add(lineDrawElements);

                    //米字格左叉线
                    lineDrawElements = new LineDrawElements();
                    lineDrawElements.setLineType(LineType.dash);
                    lineDrawElements.setColor(new Color(paper.getBoxColor().get(0),paper.getBoxColor().get(1),paper.getBoxColor().get(2)));
                    lineDrawElements.setPenWidth(0.5f);
                    lineDrawElements.setX(leftX + drawWordsCount * boxWidth + drawCYCount * cyJJ + boxWidth);
                    lineDrawElements.setY(initY);
                    lineDrawElements.setToX(leftX + drawWordsCount * boxWidth + drawCYCount * cyJJ);
                    lineDrawElements.setToY(initY + boxHeight);
                    drawElements.add(lineDrawElements);
                }



                //拼音
                pyTextDrawElements = new TextDrawElements();
                pyTextDrawElements.setText(this.paper.getPinyinList().get(i).get(j));
                pyTextDrawElements.setColor(Color.BLACK);
                pyTextDrawElements.setFontType(FontType.拼音);
                pyTextDrawElements.setX(leftX + drawWordsCount * boxWidth + drawCYCount * cyJJ + 18);
                pyTextDrawElements.setY(initY - 20);
                drawElements.add(pyTextDrawElements);

                if (paper.getExamType() == PaperType.answer) {
                    hzTextDrawElements = new TextDrawElements();
                    hzTextDrawElements.setText(this.paper.getWordList().get(i).get(j));
                    hzTextDrawElements.setColor(new Color(192,106,106));
                    hzTextDrawElements.setFontType(FontType.方格字);
                    hzTextDrawElements.setX(leftX + drawWordsCount * boxWidth + drawCYCount * cyJJ + 18);
                    hzTextDrawElements.setY(initY + 2);
                    drawElements.add(hzTextDrawElements);
                }


                drawWordsCount++;
            }
            drawCYCount++;
        }

        if (paper.getExamType() == PaperType.examAndAnswer) {
            drawElementsList.add(drawElements);
            drawElements = new ArrayList<>();
            drawWordsCount = 0;
            drawCYCount = 0;
            drawRowIndex = 1;

            //试卷标题
            textDrawElements = new TextDrawElements();
            textDrawElements.setText(paper.getTitle());
            textDrawElements.setFontType(FontType.大标题);
            textDrawElements.setColor(Color.BLACK);
            textDrawElements.setX(-99);
            textDrawElements.setY(20);
            drawElements.add(textDrawElements);

            textDrawElements = new TextDrawElements();
            textDrawElements.setText("答案");
            textDrawElements.setFontType(FontType.大标题);
            textDrawElements.setColor(Color.BLACK);
            textDrawElements.setX(-99);
            textDrawElements.setY(50);
            drawElements.add(textDrawElements);
            initY = 100;

            for (int i = 0; i < this.paper.getPinyinList().size(); i++) {
                for (int j = 0; j < this.paper.getWordList().get(i).size(); j++) {

                    if (j == 0 && drawWordsCount + this.paper.getWordList().get(i).size() > 10) {//换行
                        drawWordsCount = 0;
                        drawCYCount = 0;
                        drawRowIndex++;
                        initY = initY + boxHeight + rowJJ;
                        if (drawRowIndex > maxRowCount) {
                            drawRowIndex = 0;
                            initY = 30;
                            drawElementsList.add(drawElements);
                            drawElements = new ArrayList<>();
                        }
                    }


                    //方格
                    rectangleDrawElements = new RectangleDrawElements();
                    rectangleDrawElements.setWidth(boxWidth);
                    rectangleDrawElements.setHeight(boxHeight);
                    rectangleDrawElements.setColor(new Color(paper.getBoxColor().get(0),paper.getBoxColor().get(1),paper.getBoxColor().get(2)));
                    rectangleDrawElements.setPenWidth(1);
                    rectangleDrawElements.setX(leftX + drawWordsCount * boxWidth + drawCYCount * cyJJ);
                    rectangleDrawElements.setY(initY);
                    drawElements.add(rectangleDrawElements);

                    if(paper.getBoxType() == 2 || paper.getBoxType() == 3) {

                        //田字格横线
                        lineDrawElements = new LineDrawElements();
                        lineDrawElements.setLineType(LineType.dash);
                        lineDrawElements.setColor(new Color(paper.getBoxColor().get(0),paper.getBoxColor().get(1),paper.getBoxColor().get(2)));
                        lineDrawElements.setPenWidth(0.5f);
                        lineDrawElements.setX(leftX + drawWordsCount * boxWidth + drawCYCount * cyJJ);
                        lineDrawElements.setY(initY + boxHeight/2);
                        lineDrawElements.setToX(lineDrawElements.getX() + boxWidth);
                        lineDrawElements.setToY(initY + boxHeight/2);
                        drawElements.add(lineDrawElements);

                        //田字格竖线
                        lineDrawElements = new LineDrawElements();
                        lineDrawElements.setLineType(LineType.dash);
                        lineDrawElements.setColor(new Color(paper.getBoxColor().get(0),paper.getBoxColor().get(1),paper.getBoxColor().get(2)));
                        lineDrawElements.setPenWidth(0.5f);
                        lineDrawElements.setX(leftX + drawWordsCount * boxWidth + drawCYCount * cyJJ + boxWidth/2);
                        lineDrawElements.setY(initY);
                        lineDrawElements.setToX(lineDrawElements.getX());
                        lineDrawElements.setToY(initY + boxHeight);
                        drawElements.add(lineDrawElements);
                    }


                    if(paper.getBoxType() == 3){
                        //米字格右叉线
                        lineDrawElements = new LineDrawElements();
                        lineDrawElements.setLineType(LineType.dash);
                        lineDrawElements.setColor(new Color(paper.getBoxColor().get(0),paper.getBoxColor().get(1),paper.getBoxColor().get(2)));
                        lineDrawElements.setPenWidth(0.5f);
                        lineDrawElements.setX(leftX + drawWordsCount * boxWidth + drawCYCount * cyJJ);
                        lineDrawElements.setY(initY);
                        lineDrawElements.setToX(lineDrawElements.getX() + boxWidth);
                        lineDrawElements.setToY(initY + boxHeight);
                        drawElements.add(lineDrawElements);

                        //米字格左叉线
                        lineDrawElements = new LineDrawElements();
                        lineDrawElements.setLineType(LineType.dash);
                        lineDrawElements.setColor(new Color(paper.getBoxColor().get(0),paper.getBoxColor().get(1),paper.getBoxColor().get(2)));
                        lineDrawElements.setPenWidth(0.5f);
                        lineDrawElements.setX(leftX + drawWordsCount * boxWidth + drawCYCount * cyJJ + boxWidth);
                        lineDrawElements.setY(initY);
                        lineDrawElements.setToX(leftX + drawWordsCount * boxWidth + drawCYCount * cyJJ);
                        lineDrawElements.setToY(initY + boxHeight);
                        drawElements.add(lineDrawElements);
                    }


                    //拼音
                    pyTextDrawElements = new TextDrawElements();
                    pyTextDrawElements.setText(this.paper.getPinyinList().get(i).get(j));
                    pyTextDrawElements.setColor(Color.BLACK);
                    pyTextDrawElements.setFontType(FontType.拼音);
                    pyTextDrawElements.setX(leftX + drawWordsCount * boxWidth + drawCYCount * cyJJ + 18);
                    pyTextDrawElements.setY(initY - 20);
                    drawElements.add(pyTextDrawElements);

                    if (paper.getExamType() == PaperType.examAndAnswer) {
                        hzTextDrawElements = new TextDrawElements();
                        hzTextDrawElements.setText(this.paper.getWordList().get(i).get(j));
                        hzTextDrawElements.setColor(new Color(192,106,106));
                        hzTextDrawElements.setFontType(FontType.方格字);
                        hzTextDrawElements.setX(leftX + drawWordsCount * boxWidth + drawCYCount * cyJJ + 18);
                        hzTextDrawElements.setY(initY + 2);
                        drawElements.add(hzTextDrawElements);
                    }


                    drawWordsCount++;
                }
                drawCYCount++;
            }

        }

        drawElementsList.add(drawElements);


        return this;
    }
}
